<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="科目" prop="subjectId">
        <treeselect v-model="queryParams.params.subjectId" :options="subjectOptions" :normalizer="normalizer" placeholder="请选择科目" style="width:240px"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="studyWrongList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="题目" align="center" prop="question.title" />
      <el-table-column label="科目" align="center" prop="question.subjectName" />
      <el-table-column label="错误次数" align="center" prop="count" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 题目详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <question :form="form" ref="question" v-if="form.id"></question>
    </el-dialog>
  </div>
</template>

<script>
import { myStudyWrong, delStudyWrong } from "@/api/study/studyWrong";
import { getStudyQuestion } from "@/api/study/studyQuestion";
import { listStudySubject} from "@/api/study/studySubject";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import Question from "@/components/Exam/Question.vue";

export default {
  name: "MyStudyWrong",
  components: {
    Question, Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 错题本表格数据
      studyWrongList: [],
      // 科目树选项
      subjectOptions: [],
      // 弹出层标题
      title: "题目详情",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        params:{
          subjectId:null
        },
        userId: null,
        questionId: null
      },
      form: {}
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    /** 查询错题本列表 */
    getList() {
      this.loading = true;
      myStudyWrong(this.queryParams).then(response => {
        this.studyWrongList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 转换科目数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.title,
        children: node.children
      };
    },
    /** 查询科目下拉树结构 */
    getTreeselect() {
      listStudySubject().then(response => {
        this.subjectOptions = [];
        const data = { id: 0, title: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "pid");
        this.subjectOptions.push(data);
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除选中的错题本数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStudyWrong(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    handleView(row) {
      getStudyQuestion(row.questionId).then(response => {
        if(response.data.type == '2'){
          response.data.answers = [];
        }else{
          response.data.answers = null;
        }
        response.data.check = true;

        this.form = response.data;
        this.open = true;
      });
    }
  }
};
</script>
